/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.payment.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.modules.ar.entity.Tar;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.fixedexpense.FixedExpense;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.fixedexpense.FixedExpenseService;
import com.jeesite.modules.base.service.supplier.SupplierService;

import com.jeesite.modules.financing.service.finserviceorder.YYService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.pay.entity.Tpayment;
import com.jeesite.modules.pay.entity.TpaymentDetail;
import com.jeesite.modules.payment.dao.TapDao;
import com.jeesite.modules.payment.entity.TapDetail;

import com.jeesite.modules.prepay.entity.TprepaymentEstimated;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;

import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.payment.service.TapService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 应付单Controller
 * @author mr
 * @version 2021-03-11
 */
@Controller
@RequestMapping(value = "${adminPath}/payment/tap")
public class TapController extends BaseController {

	@Autowired
	private TapService tapService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private FixedExpenseService fixedExpenseService;
	@Autowired
	private TcarService tcarService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private YYService yyService;
	@Autowired
	private TapDao tapDao;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Tap get(String apId, boolean isNewRecord) {
		return tapService.get(apId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("payment:tap:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tap tap, Model model) {
		List frameNumberList = new ArrayList();
		String frameNumber = null;
		model.addAttribute("tap", tap);
		//供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList",supplierList);
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList",serviceOrderList);
		// 车辆
		List<Tcar> tcarList = tcarService.findList(new Tcar());
		Tcar tcar = new Tcar();
		tcar.setCarName("");
		tcar.setCarId("");
		tcarList.add(tcar);
		model.addAttribute("tcarList", tcarList);
		model.addAttribute("listUser", empUserService.findList(new EmpUser()));
		//车架号
		for (int i = 0; i < serviceOrderList.size(); i++) {
			List<ServiceOrderDetail> serviceOrderDetailList = serviceOrderList.get(i).getServiceOrderDetailList();
			if (serviceOrderDetailList.size() > 0) {
				frameNumber = serviceOrderDetailList.get(i).getFrameNumber();
			}else {
				frameNumber = " ";
			}
			frameNumberList.add(frameNumber);
		}
		model.addAttribute("frameNumberList",frameNumberList);
		return "modules/payment/tapList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("payment:tap:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Tap> listData(Tap tap, HttpServletRequest request, HttpServletResponse response) {
		tap.setPage(new Page<>(request, response));
		Page<Tap> page = tapService.findPage(tap);
		/*for (Tap tar1 : page.getList()) {
			if(tar1.getParentBillType()!=null&&
					!"".equals(tar1.getParentBillType())
					&&tar1.getParentBillCode()!=null&&!"".equals(tar1.getParentBillCode())) {
				String billCodeById = yyService.getBillCodeById(tar1.getParentBillType(), tar1.getParentBillCode());
				tar1.setParentBillCode1(tar1.getParentBillCode());
				tar1.setParentBillCode(billCodeById);
			}
		}*/
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("payment:tap:view")
	@RequestMapping(value = "form")
	public String form(Tap tap, Model model) {

		if(tap.getIsNewRecord()){
			model.addAttribute("rowNumberList", ListUtils.newArrayList());
		} else {
			List<TapDetail> rowNumberList =new ArrayList<>();
			for (TapDetail tapDetail : tap.getTapDetailList()) {
				rowNumberList.add(tapDetail);
			}
			model.addAttribute("rowNumberList", rowNumberList);
		}


		model.addAttribute("tap", tap);
		//供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList",supplierList);
		if(!tap.getIsNewRecord()){
			List<ServiceOrder> serviceOrderList =new ArrayList<>();
			ServiceOrder serviceOrder =new ServiceOrder();
			serviceOrder.setServiceOrderId(tap.getServiceOrderId());
			serviceOrder=serviceOrderService.get(serviceOrder);
			serviceOrderList.add(serviceOrder);

			model.addAttribute("serviceOrderList",serviceOrderList);
		}else{
			model.addAttribute("serviceOrderList", ListUtils.newArrayList());
		}
		List<ServiceOrderDetail> frameNumberList = this.getFrameNumberList(tap.getServiceOrderId());
		model.addAttribute("frameNumberList",frameNumberList);
		return "modules/payment/tapForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("payment:tap:view")
	@RequestMapping(value = "detail")
	public String detail(Tap tap, Model model) {

		Double apAmount = 0D;
		FixedExpense fixedExpense =null;
		for (TapDetail tapDetail:tap.getTapDetailList()) {
			fixedExpense=new FixedExpense();
			fixedExpense.setFixedExpenseId(tapDetail.getExpenseItem());
			fixedExpense =fixedExpenseService.get(fixedExpense);
			if(fixedExpense!=null){
				tapDetail.setExpenseItemName(fixedExpense.getFixedExpenseName());
			}
			if (tapDetail.getApAmount() != null && tapDetail.getApAmount() != ' ') {
				apAmount = NumberUtils.add(apAmount,tapDetail.getApAmount());
			}
		}
		tap.setTotalAmount(apAmount);
		tap.setAmount(apAmount);
		if(tap.getParentBillType()!=null&&tap.getParentBillCode()!=null&&!"".equals(tap.getParentBillType())&&!"".equals(tap.getParentBillCode())) {
			String billCodeById = yyService.getBillCodeById(tap.getParentBillType(), tap.getParentBillCode());
			tap.setParentBillCode1(billCodeById);
		}
		model.addAttribute("tap", tap);
		//供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList",supplierList);
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList",serviceOrderList);

		return "modules/payment/tapDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("payment:tap:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Tap tap) {

		tapService.save(tap);
		return renderResult(Global.TRUE, text("保存应付单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("payment:tap:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Tap tap) {
		tapService.delete(tap);

		return renderResult(Global.TRUE, text("删除应付单成功！"));
	}

	/**
	 * 查询车架号
	 * @param serviceOrderId
	 * @return
	 */
	@RequestMapping(value = "getFrameNumberList")
	@ResponseBody
	public List<ServiceOrderDetail> getFrameNumberList(@RequestParam("serviceOrderId") String serviceOrderId){
		List<ServiceOrderDetail> frameNumberList = tapService.getFrameNumber(serviceOrderId);
		ServiceOrderDetail serviceOrderDetail = new ServiceOrderDetail();
		serviceOrderDetail.setFrameNumber("");
		frameNumberList.add(serviceOrderDetail);
		return frameNumberList;
	}
	/**
	 * 查询车架号
	 * @param serviceOrderCode
	 * @return
	 */
	@RequestMapping(value = "getFrameNumberList2")
	@ResponseBody
	public List<ServiceOrderDetail> getFrameNumberList2(@RequestParam("serviceOrderId") String serviceOrderCode){
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderCode(serviceOrderCode);
		serviceOrder=serviceOrderService.findList(serviceOrder).get(0);

		List<ServiceOrderDetail> frameNumberList = tapService.getFrameNumber(serviceOrder.getServiceOrderId());
		ServiceOrderDetail serviceOrderDetail = new ServiceOrderDetail();
		serviceOrderDetail.setFrameNumber("");
		frameNumberList.add(serviceOrderDetail);
		return frameNumberList;
	}

	/**
	 * 查询车辆信息
	 * @param frameNumber
	 * @return
	 */
	@RequestMapping(value = "getCarDetailInfo")
	@ResponseBody
	public ServiceOrderDetail getCarDetailInfo(@RequestParam("frameNumber") String frameNumber){
		ServiceOrderDetail carDetailInfo = tapService.getCarDetailInfo(frameNumber);
		return carDetailInfo;
	}

	/**
	 * 查询车辆信息
	 * @param rowNumber
	 * @param serviceOrderId
	 * @return
	 */
	@RequestMapping(value = "getCarDetailInfo1")
	@ResponseBody
	public ServiceOrderDetail getCarDetailInfo1(@RequestParam("rowNumber") Long rowNumber,@RequestParam("serviceOrderId") String serviceOrderId){
		ServiceOrderDetail carDetailInfo = new ServiceOrderDetail();
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderId(serviceOrderId);
		serviceOrder =serviceOrderService.get(serviceOrder);
		carDetailInfo.setServiceOrderId(serviceOrder);
		carDetailInfo.setRowNumber(rowNumber);

		return serviceOrderService.getDetail(carDetailInfo);
	}


	/**
	 * 财务确认
	 * @param apId
	 * @return
	 */
	@RequestMapping(value = "updateFinancialRecognition")
	@ResponseBody
	public String  updateFinancialRecognition(@RequestParam("apId") String apId,@RequestParam("sta") String sta){
		String str = "";
		int i = tapService.updateFinancialRecognition(apId,sta);
		if (i > 0){
			str = "操作成功";
		}
		return str;
	}
/**
 * 查询符合的预订单
 */
	@RequiresPermissions("payment:tap:view")
	@RequestMapping(value = "getTapList")
	@ResponseBody
	public List<TpaymentDetail> getTapList(Tap tap){

		List<Tap> tapList =tapService.findList(tap);
		List<TpaymentDetail> tpaymentDetailList =new ArrayList<>();
		for (Tap tap1 : tapList) {
			tap1 =tapService.get(tap1);
			TpaymentDetail tpaymentDetail =null;
			for (TapDetail tapDetail : tap1.getTapDetailList()) {
				tpaymentDetail =new TpaymentDetail();
				tpaymentDetail.setApCode(tap1.getApCode());
				tpaymentDetail.setRowNumber(tapDetail.getRowNumber());
				tpaymentDetail.setBrand(tapDetail.getBrand());
				tpaymentDetail.setModel(tapDetail.getModel());
				tpaymentDetail.setArea(tapDetail.getArea());
				tpaymentDetail.setCarId(tapDetail.getCarId());
				tpaymentDetail.setTcar(tapDetail.getCar());
				tpaymentDetail.setFrameNumber(tapDetail.getFrameNumber());
				tpaymentDetail.setFixedExpense(tapDetail.getFixedExpense());
				tpaymentDetail.setExpenseItem(tapDetail.getExpenseItem());
				tpaymentDetail.setPayentLeft(tapDetail.getPayLeft());
				tpaymentDetail.setApAmount(tapDetail.getApAmount());
					tapDetail.setApCode(tap1.getApCode());
				tpaymentDetailList.add(tpaymentDetail);
			}
		}
		return tpaymentDetailList;
	}

	/**
	 * 应付详细批量生成付款单
	 */
	@RequiresPermissions("payment:tap:view")
	@RequestMapping(value = "saveAll")
	public String saveAll(String str,String date,String supplierId,String serviceOrderId,String currency,String paymentMethod,String paymentBank, Model model) {

		Tpayment tpayment =new Tpayment();
		List<TpaymentDetail> tpaymentDetailList =new ArrayList<>();
		//设置应付单详情
		Tap tap =null;
		if(str.length()>1){
			String[] s = str.split(",");
			TapDetail tapDetail =null;
			TpaymentDetail tpaymentDetail= null;
			for(int i=0;i<s.length;i++){
				//获取应付单编号
				tapDetail=tapService.findById(s[i]);
				tap = tapService.selectOne(s[i]);

				tpaymentDetail =new TpaymentDetail();
				tpaymentDetail.setApCode(tap.getApCode());
				tpaymentDetail.setBrand(tapDetail.getBrand());
				tpaymentDetail.setArea(tapDetail.getArea());
				tpaymentDetail.setModel(tapDetail.getModel());
				tpaymentDetail.setCarId(tapDetail.getCarId());
				tpaymentDetail.setFrameNumber(tapDetail.getFrameNumber());
				tpaymentDetail.setExpenseItem(tapDetail.getExpenseItem());
				tpaymentDetail.setActualApAmount(tapDetail.getActualApAmount());
				tpaymentDetail.setApAmount(tapDetail.getApAmount());
				tpaymentDetail.setRowNumber(tapDetail.getRowNumber());

				if(tapDetail.getActualApAmount()==null){
					tpaymentDetail.setPayentLeft(tapDetail.getApAmount());
				}else{
					tpaymentDetail.setPayentLeft(tapDetail.getApAmount()-tapDetail.getActualApAmount());
				}

				tpaymentDetailList.add(tpaymentDetail);
			}

			//设置支付单信息
			tpayment.setTpaymentDetailList(tpaymentDetailList);
			tpayment .setCurrency(currency);
			tpayment .setServiceOrderId(serviceOrderId);
			tpayment.setSupplierId(supplierId);
			Supplier supplier =new Supplier();
			supplier.setSupplierId(supplierId);
			supplier =supplierService.get(supplier);
			tpayment .setSupplier(supplier);

			SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = null;
			try {
				date1 = simpleDateFormat.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			tpayment.setDate(date1);
		}


		//供应商
		List<Supplier> supplierList = new ArrayList<>();
		supplierList.add(tpayment.getSupplier());
		model.addAttribute("supplierList",supplierList);
		//服务订单
		List<ServiceOrder> serviceOrderList = new ArrayList<>();

		ServiceOrder serviceOrder = new ServiceOrder();
		serviceOrder.setServiceOrderId(tpayment.getServiceOrderId());
		serviceOrder =serviceOrderService.get(serviceOrder);
		serviceOrderList.add(serviceOrder);

		model.addAttribute("serviceOrderList",serviceOrderList);
		//应付单
		List<Tap> tapList =new ArrayList<>();
		//tapList.add(tap);
		model.addAttribute("tapList",tapList);

		model.addAttribute("tpayment", tpayment);
		return "modules/pay/tpaymentForm";
	}

/**
 * 获得费用项目类型
 */

@RequestMapping(value = "getExpenseItem")
@ResponseBody
public List<FixedExpense> getExpenseItem(){
	return fixedExpenseService.findList(new FixedExpense());
}

    //获得行号
	@RequestMapping(value = "getNumberList")
	@ResponseBody
	public List<Long> getNumberList(@RequestParam("serviceOrderId") String serviceOrderCode){
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderCode(serviceOrderCode);
		serviceOrder=serviceOrderService.findList(serviceOrder).get(0);
		serviceOrder =serviceOrderService.get(serviceOrder);

		List<Long> list =new ArrayList<>();
		for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
			list.add(serviceOrderDetail.getRowNumber());
		}
		return list;
	}

	//获得行号
	@RequestMapping(value = "getNumberList1")
	@ResponseBody
	public List<Long> getNumberList1(@RequestParam("serviceOrderId") String serviceOrderId){
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderId(serviceOrderId);
		serviceOrder =serviceOrderService.get(serviceOrder);

		List<Long> list =new ArrayList<>();
		for (ServiceOrderDetail serviceOrderDetail : serviceOrder.getServiceOrderDetailList()) {
			list.add(serviceOrderDetail.getRowNumber());
		}
		return list;
	}

	/**
	 * 获取通过选定客户查询应收单部分信息
	 * @param supplierId  客户id
	 */
	@RequestMapping(value = "getTapBySuId")
	@ResponseBody
	public List<ServiceOrder> getServiceOrderId(@RequestParam("supplierId") String supplierId) {
		List<ServiceOrder> strings = tapDao.selectServiceOrder(supplierId);
		return strings;
	}

}

